/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortustve;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author jphipps
 */
public class process extends ortusvars {

	public static Object execute(Object[] args) {
	    HashMap<String,String>primitives = new HashMap<String,String>();
	    primitives.put("int", "java.lang.Integer");
	    primitives.put("long", "java.lang.Long");
	    primitives.put("float","java.lang.Float");
	    primitives.put("byte","java.lang.Byte");
	    primitives.put("boolean","java.lang.Boolean");
	    primitives.put("char","java.lang.Char");
	    primitives.put("short","java.lang.Short");
	    primitives.put("double","java.lang.Double");

	    int numparms = 0;
	    List<Class> parmtypes = new ArrayList();

	    try {
		    List<Object> arglist = new ArrayList<Object>();
		    for ( int x = 1; x < args.length;x++) {
			    arglist.add(args[x]);
		    }
		    int methodoffset = ((String)args[0]).lastIndexOf(".");
		    String classname = ((String)args[0]).substring(0,methodoffset);
		    String methodname = ((String)args[0]).substring(methodoffset+1);
		    Object[] ml = null;
			ml = Class.forName(classname).getMethods();
		    Method meth = null;

		    int feildmatches = 0;
		    for ( Object y : ml ) {
		       Method z = (Method) y;
		       if ( z.getName().equals(methodname) && z.getParameterTypes().length == arglist.size()) {
			   for ( int pc = 0 ; pc < z.getParameterTypes().length; pc++) {
			      if ( z.getParameterTypes()[pc].isPrimitive())
				if ( arglist.get(pc).getClass().getName().equals(primitives.get(z.getParameterTypes()[pc].getName())))
					feildmatches++;
				else
				    if( arglist.get(pc).getClass().isInstance(z.getParameterTypes()[pc].getName()))
					    feildmatches++;
			   }
			   if ( z.getParameterTypes().length == feildmatches)
				meth = z;
		       }
		    }

		    Object retobj = null;
		    if ( meth != null)
			retobj = meth.invoke(null, arglist.toArray());
		    return retobj;
	    } catch (Exception e) {
		ortustve.api.DebugLog(TRACE2,"Process: execute: " + args[0] + " Exception: " + e);
		return null;
	    }
	}
	public static Object executeRemote(Object[] args) {
	    HashMap<String,String>primitives = new HashMap<String,String>();
	    primitives.put("int", "java.lang.Integer");
	    primitives.put("long", "java.lang.Long");
	    primitives.put("float","java.lang.Float");
	    primitives.put("byte","java.lang.Byte");
	    primitives.put("boolean","java.lang.Boolean");
	    primitives.put("char","java.lang.Char");
	    primitives.put("short","java.lang.Short");
	    primitives.put("double","java.lang.Double");

	    if ( ortustve.api.IsRemoteHost()) {
		    return ortustve.api.ExecuteRemoteCMD(args);
	    }

	    int numparms = 0;
	    List<Class> parmtypes = new ArrayList();

	    try {
		    List<Object> arglist = new ArrayList<Object>();
		    for ( int x = 1; x < args.length;x++) {
			    arglist.add(args[x]);
		    }
		    int methodoffset = ((String)args[0]).lastIndexOf(".");
		    String classname = ((String)args[0]).substring(0,methodoffset);
		    String methodname = ((String)args[0]).substring(methodoffset+1);
		    Object[] ml = null;
			ml = Class.forName(classname).getMethods();
		    Method meth = null;

		    int feildmatches = 0;
		    for ( Object y : ml ) {
		       Method z = (Method) y;
		       if ( z.getName().equals(methodname) && z.getParameterTypes().length == arglist.size()) {
			   for ( int pc = 0 ; pc < z.getParameterTypes().length; pc++) {
			      if ( z.getParameterTypes()[pc].isPrimitive())
				if ( arglist.get(pc).getClass().getName().equals(primitives.get(z.getParameterTypes()[pc].getName())))
					feildmatches++;
				else
				    if( arglist.get(pc).getClass().isInstance(z.getParameterTypes()[pc].getName()))
					    feildmatches++;
			   }
			   if ( z.getParameterTypes().length == feildmatches)
				meth = z;
		       }
		    }

		    Object retobj = null;
		    if ( meth != null)
			retobj = meth.invoke(null, arglist.toArray());
		    return retobj;
	    } catch (Exception e) {
		ortustve.api.DebugLog(TRACE2,"Process: executeRemote: " + args[0] + " Exception: " + e);
		return null;
	    }
	}
}